<?php

/* 
Plugin Name: 404 Error Log and SendMail 2
Plugin URI: http://www.traffyk.com/plugin/404-error-write-log-and-sendmail/
Description: Tracking of 404 error by saving log in a file html and send an mail to specified address (if selected).
Version: 0.3
Author: Traffyk
Author URI: http://www.traffyk.com

Copyright 2007 Traffyk  (email : traffyk@gmail.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

//Path and filename for error file. By default log.html is saved in root
$traffyk404_path = "log.html";

//Subject for mail
$traffyk404_subject = "Report 404 error found on your blog";

// Date Format, see http://www.php.net/date for another style
$traffyk404_date=date("d.m.y - G:i:s");

// Do not modify code after this line if you don't know what you're doing
##########################################

function traffyk404_writeLog() {
	global $traffyk404_path, $traffyk404_date;
	$message = "<font size=2px><b>[$traffyk404_date], [IP: <font color=CC0000>$_SERVER[REMOTE_ADDR]</font> ($_SERVER[HTTP_USER_AGENT])], <font color=006600>$_SERVER[REQUEST_URI]</font>, <font color=000099>$_SERVER[HTTP_REFERER]</font></b></font><br/>\n";
	$fp = fopen ($traffyk404_path,"a+");
	fwrite($fp, $message);
	fclose($fp);
}



function traffyk404_sendMail() {
	global $traffyk404_date, $traffyk404_subject;

	// Permalink
	$traffyk404_indirizzoemail = get_option("traffyk404_indirizzoemail");
	
	$message = "
------------------------------------------------------------------------------
Site:\t".get_bloginfo('name')." (".get_option('home').")
Error:\t404 $traffyk404_subject
Date:\t$traffyk404_date
Url:\t".get_settings('home')."$_SERVER[REQUEST_URI]
Ip address:\t$_SERVER[REMOTE_ADDR]
Browser:\t$_SERVER[HTTP_USER_AGENT]
Referer page:\t$_SERVER[HTTP_REFERER]
------------------------------------------------------------------------------";
	mail("$traffyk404_indirizzoemail", $traffyk404_subject, $message,
	"From: error404@$SERVER_NAME\r\n"
	."X-Mailer: PHP/" . phpversion());
}
	

function traffyk404() {

	// Permalink
	$traffyk404_invio_email = get_option("traffyk404_invio_email");
			
	if ($traffyk404_invio_email == "true")
		traffyk404_sendMail();
	else
		traffyk404_writeLog();
	
	if (file_exists(TEMPLATEPATH . '/404.php'))
		require_once(TEMPLATEPATH . '/404.php');
	else
		require_once(TEMPLATEPATH . '/index.php');
	die();
}




function traffyk404_options()
    {
	if($_POST['traffyk404_save'])   {
		update_option('traffyk404_invio_email', $_POST['traffyk404_invio_email']);
		update_option('traffyk404_indirizzoemail', $_POST['traffyk404_indirizzoemail']);
		echo '<div class="updated"><p>Impostazioni Traffyk 404 salvate con successo.</p></div>';       }
           ?>
 	<div class="wrap">
	<h2>Traffyk 404 Options</h2>
	<form method="post" id="traffyk404_options">
		<fieldset class="options">
		<legend>Opzioni invio rapporto</legend>

	<table width="100%" cellspacing="2" cellpadding="5" class="editform"> 

	<tr valign="top">
	<th scope="row">
	<b>Inviare resoconto errori tramite email?: </b>
	</th>
	<td>
	<select name="traffyk404_invio_email" id="traffyk404_invio_email">
	<option <?php if(get_option('traffyk404_invio_email') == 'false') { echo 'selected'; } ?> value="false">False</option>
	<option <?php if(get_option('traffyk404_invio_email') == 'true') { echo 'selected'; } ?> value="true">True</option></select>
	<br />
	Inviare rapporti tramite email?
	</td>
	</tr>

	<tr valign="top">
	<th scope="row">
	<b>Indirizzo email per invio rapporti: </b>
	</th>
	<td>
	<input name="traffyk404_indirizzoemail" type="text" id="traffyk404_indirizzoemail" value="<?php echo get_option('traffyk404_indirizzoemail') ;?>" size="40" maxlength="40" />
	<br />
	Indirizzo email per invio rapporti
	</td>
	</tr>

	</table>

	                <p class="submit"><input type="submit" name="traffyk404_save" value="Aggiorna impostazioni &raquo;" /></p>
		</fieldset>
	</form>
	</div>
             <?php
    }


function traffyk404_adminmenu(){
	add_options_page('Traffyk 404 Options', 'Traffyk404', 9, '404_error_log.php', 'traffyk404_options');
}


add_action('admin_menu','traffyk404_adminmenu',1);
add_filter('404_template', 'traffyk404');

?>